package com.ff.discounts.component.login

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import com.drake.channel.receiveEvent
import com.ff.discounts.AppContext
import com.ff.discounts.R
import com.ff.discounts.activity.BaseViewModelActivity
import com.ff.discounts.api.DefaultNetworkService
import com.ff.discounts.component.main.MainViewModel
import com.ff.discounts.databinding.ActivityLoginBinding

class LoginActivity : BaseViewModelActivity<ActivityLoginBinding>() {
    private lateinit var loginViewModel: LoginViewModel
    override fun initListeners() {
        super.initListeners()



        binding.buttonLogin.setOnClickListener{
            loginViewModel.codeLogin("15399583713","1234")
        }
        binding.buttonCode.setOnClickListener {
            loginViewModel.getCode("15399583713")
        }
        binding.loginPassword.setOnClickListener {
            loginViewModel.passwordLogin("15399583713", "123456")
        }
    }

    override fun initDatum() {
        super.initDatum()
        loginViewModel = ViewModelProvider(this)[LoginViewModel::class.java]
        initViewModel(loginViewModel)

        //观察成功结果
        loginViewModel.success.observe(this) {
            //成功了

            //执行登录后操作
            AppContext.instance.onLogin()
        }

        receiveEvent<LoginStatusChangedEvent> {
            finish()
        }
    }
}